/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved. 
 */
package com.icinfo.cloud.provider.punish.fastcase.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.common.enums.YesOrNoEnum;
import com.icinfo.cloud.provider.punish.fastcase.dto.CaseAppraiseFeedbackDto;
import com.icinfo.cloud.provider.punish.fastcase.dto.CaseAppraiseRecordByUserQueryDto;
import com.icinfo.cloud.provider.punish.fastcase.enums.CaseAppraiseStatusEnum;
import com.icinfo.cloud.provider.punish.fastcase.mapper.CaseAppraiseRecordMapper;
import com.icinfo.cloud.provider.punish.fastcase.model.CaseAppraiseRecord;
import com.icinfo.cloud.provider.punish.fastcase.service.ICaseAppraiseRecordService;
import com.icinfo.cloud.provider.punish.fastcase.service.ICaseAppraiseTemplateInfoService;
import com.icinfo.cloud.provider.punish.fastcase.vo.CaseAppraiseDetailInfoVo;
import com.icinfo.cloud.provider.punish.fastcase.vo.CaseAppraiseRecordVo;
import com.icinfo.cloud.provider.punish.ucase.casemain.enums.CaseTypeEnum;
import com.icinfo.cloud.provider.punish.ucase.casemain.mapper.CaseHandlePersonRecordMapper;
import com.icinfo.cloud.provider.punish.ucase.casemain.mapper.CaseMainDetailInfoMapper;
import com.icinfo.cloud.provider.punish.ucase.casemain.mapper.CaseMainInfoMapper;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.CaseHandlePersonRecord;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.CaseMainDetailInfo;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.CaseMainInfo;
import com.icinfo.framework.common.web.Result;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import com.icinfo.framework.mybatis.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 描述: 案件评价记录表 case_appraise_record 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2023年02月02日
 */
@Slf4j
@Service
public class CaseAppraiseRecordServiceImpl extends MyBatisServiceSupport implements ICaseAppraiseRecordService {

    @Resource
    private CaseAppraiseRecordMapper caseAppraiseRecordMapper;
    @Resource
    private CaseHandlePersonRecordMapper caseHandlePersonRecordMapper;
    @Resource
    private CaseMainInfoMapper caseMainInfoMapper;
    @Resource
    private CaseMainDetailInfoMapper caseMainDetailInfoMapper;
    @Resource
    private ICaseAppraiseTemplateInfoService caseAppraiseTemplateInfoService;

    /**
     * 描述： 查询案件评价记录
     *
     * @author: zhengqiang
     * @date: 2023/2/3
     * @param: queryDto
     * @return {@link List< CaseAppraiseRecordVo>}
     **/
    @Override
    public List<CaseAppraiseRecordVo> getCaseAppraiseRecordPageList(CaseAppraiseRecordByUserQueryDto queryDto) {

        PageHelper.startPage(queryDto.getPageNum(), queryDto.getLength());
        List<CaseAppraiseRecordVo> caseAppraiseRecordVoList = caseAppraiseRecordMapper.selectCaseAppraiseRecordPageList(queryDto);
        caseAppraiseRecordVoList.forEach(e -> {
            String caseId = e.getCaseId();
            //1.循环设置处理人员
            Example example = new Example(CaseHandlePersonRecord.class);
            example.createCriteria().andEqualTo("caseId", caseId).andEqualTo("status", Constant.STRING_1);
            List<CaseHandlePersonRecord> handlePersonRecords = caseHandlePersonRecordMapper.selectByExample(example);
            for (CaseHandlePersonRecord caseHandlePersonRecord : handlePersonRecords) {
                if (Constant.STRING_1.equals(caseHandlePersonRecord.getHandlePersonType())) {
                    e.setMainHandlePerson(caseHandlePersonRecord.getHandlePersonName());
                } else if (Constant.STRING_2.equals(caseHandlePersonRecord.getHandlePersonType())) {
                    e.setHelpHandlePerson(caseHandlePersonRecord.getHandlePersonName());
                }
            }
            e.setCaseTypeName(CaseTypeEnum.getValueByCode(e.getCaseType()));
            //设置评价状态
            e.setAppraiseStatusName(CaseAppraiseStatusEnum.getValueByCode(e.getAppraiseStatus()));
            e.setIsExistExceptionName(YesOrNoEnum.getValueByCode(e.getIsExistException()));
        });

        return caseAppraiseRecordVoList;
    }

    /**
     * 描述： 根据案件id获取案件评价详情
     *
     * @author: zhengqiang
     * @date: 2023/2/3
     * @param: caseId
     * @return {@link CaseAppraiseDetailInfoVo}
     **/
    @Override
    public CaseAppraiseDetailInfoVo getCaseAppraiseRecordDetailByCaseId(String caseId) {
        CaseAppraiseDetailInfoVo detailInfoVo = new CaseAppraiseDetailInfoVo();
        //查询案件信息
        CaseMainInfo caseMainInfo = caseMainInfoMapper.selectByPrimaryKey(caseId);
        detailInfoVo.setCaseId(caseId);
        detailInfoVo.setCaseName(caseMainInfo.getCaseName());
        detailInfoVo.setCaseNumber(caseMainInfo.getCaseNumber());
        detailInfoVo.setCaseRegistTime(caseMainInfo.getCaseRegistTime());
        detailInfoVo.setCaseEndTime(caseMainInfo.getCaseEndTime());
        detailInfoVo.setHandleOrgName(caseMainInfo.getCaseOrgName());
        detailInfoVo.setCaseType(caseMainInfo.getCaseType());
        detailInfoVo.setCaseAppraiseTitle(caseMainInfo.getCaseOrgName() + "邀请您对“" + caseMainInfo.getCaseName() + "”办理过程进行评价");

        CaseMainDetailInfo caseMainDetailInfo = caseMainDetailInfoMapper.selectByPrimaryKey(caseId);
        detailInfoVo.setCaseReason(caseMainDetailInfo.getCaseReason());
        detailInfoVo.setCaseHappenAddressDetail(caseMainDetailInfo.getCaseHappenAddressDetail());
        detailInfoVo.setPunishContent(caseMainDetailInfo.getPunishContent());
        detailInfoVo.setIllegalFact(caseMainDetailInfo.getIllegalFact());

        //设置办案人员
        Example example = new Example(CaseHandlePersonRecord.class);
        example.createCriteria().andEqualTo("caseId", caseId).andEqualTo("status",Constant.STRING_1);
        example.orderBy("handlePersonType").asc();
        List<CaseHandlePersonRecord> handlePersonRecords = caseHandlePersonRecordMapper.selectByExample(example);
        String handPersonName = handlePersonRecords.stream().map(p -> p.getHandlePersonName()).collect(Collectors.joining("、"));
        for (CaseHandlePersonRecord caseHandlePersonRecord : handlePersonRecords) {
            if (Constant.STRING_1.equals(caseHandlePersonRecord.getHandlePersonType())) {
                detailInfoVo.setMainHandlePerson(caseHandlePersonRecord.getHandlePersonName());
            } else if (Constant.STRING_2.equals(caseHandlePersonRecord.getHandlePersonType())) {
                detailInfoVo.setHelpHandlePerson(caseHandlePersonRecord.getHandlePersonName());
            }
        }
        detailInfoVo.setHandlePersonName(handPersonName);

        //查询评价信息
        Example appraiseExample = new Example(CaseAppraiseRecord.class);
        appraiseExample.createCriteria().andEqualTo("caseId", caseId);
        List<CaseAppraiseRecord> caseAppraiseRecords = caseAppraiseRecordMapper.selectByExample(appraiseExample);
        if (!ObjectUtils.isEmpty(caseAppraiseRecords)) {
            CaseAppraiseRecord caseAppraiseRecord = caseAppraiseRecords.get(0);
            JSONObject jsonObject = JSON.parseObject(caseAppraiseRecord.getAppraiseContent());
            Map valueMap = JSON.parseObject(jsonObject.getString("valueMap"), Map.class);
            detailInfoVo.setValueMap(valueMap);
            detailInfoVo.setAppraiseStatus(caseAppraiseRecord.getAppraiseStatus());
            detailInfoVo.setAppraiseStatusText(CaseAppraiseStatusEnum.getValueByCode(caseAppraiseRecord.getAppraiseStatus()));
            detailInfoVo.setAppraiseFeedbackContent(caseAppraiseRecord.getFeedbackContent());
        } else {
            Map defaultValueMap = caseAppraiseTemplateInfoService.getCaseAppraiseTemplateDefaultValueMap(caseMainInfo.getCaseType());
            detailInfoVo.setValueMap(defaultValueMap);
            detailInfoVo.setAppraiseStatus(Constant.STRING_0);
            detailInfoVo.setAppraiseStatusText("待评价");
        }

        return detailInfoVo;
    }

    /**
     * 描述： 更新评价反馈信息
     *
     * @author: zhengqiang
     * @date: 2023/2/6
     * @param: caseAppraiseFeedbackDto
     * @return {@link Result}
     **/
    @Override
    public Result updateCaseFeedback(CaseAppraiseFeedbackDto caseAppraiseFeedbackDto) {
        Example appraiseExample = new Example(CaseAppraiseRecord.class);
        appraiseExample.createCriteria().andEqualTo("caseId", caseAppraiseFeedbackDto.getCaseId());

        CaseAppraiseRecord caseAppraiseRecord = new CaseAppraiseRecord();
        if (Constant.STRING_1.equals(caseAppraiseFeedbackDto.getAppraiseStatus())) {
            caseAppraiseRecord.setAppraiseStatus(CaseAppraiseStatusEnum.INVESTIGATION.getCode());
        } else {
            caseAppraiseRecord.setAppraiseStatus(CaseAppraiseStatusEnum.FEEDBACK.getCode());
            caseAppraiseRecord.setFeedbackContent(caseAppraiseFeedbackDto.getFeedbackContent());
            caseAppraiseRecord.setFeedbackTime(new Date());
        }
        caseAppraiseRecordMapper.updateByExampleSelective(caseAppraiseRecord, appraiseExample);
        return Result.success(Constant.STRING_SUCCESS_EN);
    }
}